package com.zxl.live.screen.screen.utils;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;

import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.util.HashMap;

public class BitmapLoaderUtils {
	
	private Context mContext;
	
	private Options mOptions;
	
	private static final int INIT_BITMAP_CAPACITY = 50;
	
	private HashMap<String, SoftReference<Bitmap>> mMiddleCache;
	
	private HashMap<String, String> mMiddleBitmapToNameMap;
	
	private static BitmapLoaderUtils sInstance;
	
	private BitmapLoaderUtils(Context mContext) {
		this.mContext = mContext;
		mOptions = new Options();
		mOptions.inPreferredConfig = Bitmap.Config.RGB_565;
		mOptions.inPurgeable = true;
		mOptions.inInputShareable = true;
		
		mMiddleCache = new HashMap<String, SoftReference<Bitmap>>(INIT_BITMAP_CAPACITY);
		
		mMiddleBitmapToNameMap = new HashMap<String, String>(INIT_BITMAP_CAPACITY);
	}
	
	public static BitmapLoaderUtils getsInstance(Context mContext) {
		if (null == sInstance) {
			sInstance = new BitmapLoaderUtils(mContext);
		}
		return sInstance;
	}
	
	/**
	 * <br>
	 * 功能简述:销毁中间层bitmap <br>
	 * 注意:罩子层和中间层已分开
	 */
	public void recyleAllMiddleBitmap() {
		if (mMiddleCache != null) {
			for (SoftReference<Bitmap> reference : mMiddleCache.values()) {
				if (reference != null) {
					Bitmap bitmap = reference.get();
					if (bitmap != null && !bitmap.isRecycled()) {
						bitmap.recycle();
						bitmap = null;
					}
				}
			}
			mMiddleCache.clear();
		}
		if (mMiddleBitmapToNameMap != null) {
			mMiddleBitmapToNameMap.clear();
		}
	}

	/**
	 * 从raw文件夹获取原图
	 * 
	 * @return
	 */
	public Bitmap getBitmap(String drawableName) {
		if (drawableName == null || mContext == null) {
			return null;
		}
		Bitmap bitmap = null;
		SoftReference<Bitmap> bitmapReference = mMiddleCache.get(drawableName);
		if (bitmapReference != null) {
			bitmap = bitmapReference.get();
			if (bitmap != null) {
				return bitmap;
			}
		}
		try {
			Resources res = mContext.getResources();
			int resId = res.getIdentifier(drawableName, "raw", mContext.getPackageName());
			bitmap = redBitmapByRaw(resId);
			mMiddleCache.put(drawableName, new SoftReference<Bitmap>(bitmap));
			mMiddleBitmapToNameMap.put(bitmap.toString(), drawableName);
		} catch (NotFoundException e) {
			e.printStackTrace();
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bitmap;
	}

	/**
	 * 专为背景图设计
	 * 
	 * @param drawableName
	 * @return
	 */
	public Bitmap getScaledBitmap(String drawableName) {
		if (drawableName == null || mContext == null) {
			return null;

		}
		Bitmap bitmap = null;
		SoftReference<Bitmap> bitmapReference = mMiddleCache.get(drawableName);
		if (bitmapReference != null) {
			bitmap = bitmapReference.get();
			if (bitmap != null) {
				return bitmap;
			}
		}
		try {
			Resources res = mContext.getResources();
			int resId = res.getIdentifier(drawableName, "raw", mContext.getPackageName());
			bitmap = redBitmapByRaw(resId);
			if (bitmap != null) {
				bitmap = Bitmap.createScaledBitmap(bitmap, ScreenUtils.sWidthPixels, ScreenUtils.sHeightPixels, true);
			}
			mMiddleCache.put(drawableName, new SoftReference<Bitmap>(bitmap));
			mMiddleBitmapToNameMap.put(bitmap.toString(), drawableName);
		} catch (NotFoundException e) {
			e.printStackTrace();
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bitmap;
	}
	
	private Bitmap redBitmapByRaw(int resId) {
		InputStream inputStream = mContext.getResources().openRawResource(resId);
		try {
			return BitmapFactory.decodeStream(inputStream, null, mOptions);
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
